import { Component, OnInit } from '@angular/core';
import * as dayjs from 'dayjs';
import { NzMessageService } from 'ng-zorro-antd';
import { BaseService, DataRegisterService } from 'src/app/services';
import { EventManagerService } from 'src/app/services/event-manager.service';
import { MessageService } from 'src/app/services/message.service';
import { OrganizationService } from 'src/app/services/organization.service';
import { UploadService } from 'src/app/services/upload.service';
import { EngineService } from '../../../services/engine.service';
import { BaseFieldComponent } from '../base-field/base-field.component';

@Component({
  selector: 'zepride-short-message',
  templateUrl: './short-message.component.html',
  styleUrls: ['./short-message.component.less']
})
export class ShortMessageComponent extends BaseFieldComponent implements OnInit {
  spaceChars: SpaceCharItem[] = [];
  receiverList: Receiver[] = [];

  constructor(
    protected engineService: EngineService,
    protected baseSvr: BaseService,
    protected eventSvr: EventManagerService,
    protected orgSvr: OrganizationService,
    public uploadService: UploadService,
    protected msg: NzMessageService,
    protected messageSvr: MessageService,
    protected dataRegisterService: DataRegisterService
  ) {
    super(engineService, baseSvr, eventSvr, orgSvr, uploadService, msg, dataRegisterService);
  }

  ngOnInit() {
    console.log('ShortMessageComponent', this.componentData);
    this.addRow();
  }

  addRow() {
    let receiver = new Receiver();
    this.receiverList.push(receiver);
  }

  delRow(index: number) {
    this.receiverList.splice(index, 1);
  }

  private async replaceTxt() {
    let txt: string = this.componentData.extends.txtAreaContent;
    for (const item of this.spaceChars) {
      if (item.selector == 'single-date' || item.selector == 'single-date-cur') {
        // 如果选择的是时间组件或者选择的是当前时间
        let dateObject = this.dataModel[item.bindField];
        if (!dateObject) {
          continue;
        }
        let repTxt = dateObject.display;
        if (item.addTime) {
          if (item.addTimeType == 'normalDay') {
            repTxt = dayjs(dateObject).add(item.addTime, 'day').format('YYYY-MM-DD HH:mm');
          }
          else if (item.addTimeType == 'workDay') {
            repTxt = await this.messageSvr.getAfterWorkdayDate({
              fromDay: dateObject.date.toString(),
              days: item.addTime
            });
          }
          else if (item.addTimeType == 'minutes') {
            repTxt = dayjs(dateObject).add(item.addTime, 'minute').format('YYYY-MM-DD HH:mm');
          }
        }

        txt = txt.replace(item.code, repTxt);
      }
      else {
        let repTxt = this.dataModel[item.bindField];
        repTxt || (repTxt = '【】');
        txt = txt.replace(item.code, repTxt);
      }

    }

    return txt;
  }

  pageSavedHandle = async () => {
    const msgBody = await this.replaceTxt();


    // const orgId = localStorage.getItem('orgid');
    // const userInfo = JSON.parse(localStorage.getItem('userInfo'));
    // const curWf = this.workflowSvr.CurrentWorkflow;

    // this.userIds || (this.userIds = []);
    // this.userIds.push(userInfo.id);

    // let messageObj: Message = {
    //   id: Md5.hashStr(this.tmpComItemId + this.workflowSvr.CurrentDataRegisterId) as string,
    //   name: '',
    //   title: `【${curWf.name}】项目预警`,
    //   body: msgBody,
    //   type: '项目预警',
    //   orgId,
    //   senderId: userInfo.id,
    //   sender: userInfo,
    //   objectId: curWf.id,
    //   objectType: 'Workflow',
    //   receiverIds: this.userIds,
    //   dataRegisterId: this.workflowSvr.CurrentDataRegisterId
    // }

    // await this.messageSvr.createMessage(messageObj);

  }

}

interface SpaceCharItem {
  index: number;
  code: string;
  bindField?: string;
  tmpComItemId?: string;
  selector?: string;
  addTime?: number;
  addTimeType?: string;
}

class Receiver {
  name: string;
  phone: string;
}